package com.banesco.precargadepositos.interfaz.componentes;

import javax.microedition.lcdui.Font;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Characters;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.FontFamily;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.TouchEvent;
import net.rim.device.api.ui.Touchscreen;
import net.rim.device.api.ui.component.LabelField;

public class ElementoListaBanesco extends Field {

	public static int DRAWPOSITION_TOP = 0;
	public static int DRAWPOSITION_BOTTOM = 1;
	public static int DRAWPOSITION_MIDDLE = 2;
	public static int DRAWPOSITION_SINGLE = 3;
	private int _tamanofuente;
	private String fontFamily = "BBAlpha Sans";

	private static final int CORNER_RADIUS = 0;

	private static final int HPADDING = Display.getWidth() <= 320 ? 6 : 8;
	private static final int VPADDING = 8;

	private static final int COLOR_BACKGROUND = 0xFFFFFF;
	private static final int COLOR_BORDER = 0xBBBBBB;
	private static final int COLOR_BACKGROUND_FOCUS = 0x186DEF;

	private MyLabelField _labelField;
	private MyLabelField _labelField2;
	private Bitmap _leftIcon;
	private Bitmap _actionIcon;

	private int _targetHeight;
	private int _rightOffset;
	private int _leftOffset;

	private int _rightOffset1;
	private int _leftOffset1;
	private int _labelHeightTexto1;
	private int _labelHeightTexto2;
	private int _paddingEntreTexto = 4;

	private int _drawPosition = -1;
	private int _bitmapHeight;

	private int _fondo = 0XEBEBEB;
	private int _fondoSelected = 0xECECEC;

	private boolean _esdobleFila;
	private boolean _esPar;
	private boolean _textoResaltado;



	public ElementoListaBanesco(String label, long style) {
		this(label, null, style);
	}

	public ElementoListaBanesco(String label, Bitmap actionIcon) {
		this(label, actionIcon, 0);
	}

	public ElementoListaBanesco(String label, Bitmap actionIcon, long style) {
		this(null, label, actionIcon, style);
	}

	public ElementoListaBanesco(Bitmap icon, String label, long style) {
		this(icon, label, null, style);
	}

	public ElementoListaBanesco(Bitmap icon, String label, Bitmap actionIcon,
			long style) {
		super(USE_ALL_WIDTH | Field.FOCUSABLE);

		_labelField = new MyLabelField(label, style,false);
		_actionIcon = actionIcon;
		_leftIcon = icon;
	}

	public ElementoListaBanesco(String label, String label1, long style,
			long style1, FieldChangeListener evento, boolean esPar, boolean esDobleFila, boolean textoResaltado) {
		super(USE_ALL_WIDTH | Field.FOCUSABLE);

		_esdobleFila = esDobleFila;
		_esPar = esPar;
		_textoResaltado = textoResaltado;
		_labelField = new MyLabelField(label, style,textoResaltado);
		_labelField2 = new MyLabelField(label1, style1,false);

		setChangeListener(evento);
	}


	public ElementoListaBanesco(String label, String label1, FieldChangeListener evento, boolean esPar,boolean esDobleFila) {
		super(USE_ALL_WIDTH | Field.FOCUSABLE);

		_esdobleFila = esDobleFila;
		_esPar = esPar;
		_labelField = new MyLabelField(label, USE_ALL_WIDTH, true);
		_labelField2 = new MyLabelField(label1, USE_ALL_WIDTH, false);

		setChangeListener(evento);
	}


	/**
	 * DRAWPOSITION_TOP | DRAWPOSITION_BOTTOM | DRAWPOSITION_MIDDLE Determins
	 * how the field is drawn (borders) If none is set, then no borders are
	 * drawn
	 */
	public void setDrawPosition(int drawPosition) {
		_drawPosition = drawPosition;
	}
	
	
	public void setLeftIcon(Bitmap bitmap)
	{
		_leftIcon = bitmap;
	}

	
	public String toString() {
		return _labelField.toString();
	}

	
	public void layout(int width, int height) {

		//		_targetHeight = getFont().getHeight() / 2 * 3 + 2 * VPADDING;
		_targetHeight = getFont().getHeight() + 15;
		// #ifndef VER_4.6.1 | VER_4.6.0 | VER_4.5.0 | VER_4.2.1 | VER_4.2.0
		if (Touchscreen.isSupported()) {
			//			_targetHeight = getFont().getHeight() * 2 + 2 * VPADDING;
			_targetHeight = getFont().getHeight() + 15;
		}
		// #endif

		_leftOffset = HPADDING;
		_leftOffset1 = HPADDING;
		if (_leftIcon != null) {
			_leftOffset += _leftIcon.getWidth() + HPADDING;
			_leftOffset1 += _leftIcon.getWidth() + HPADDING;
		}

		_rightOffset = HPADDING;
		_rightOffset1 = HPADDING;
		if (_actionIcon != null) {
			_rightOffset += _actionIcon.getWidth() + HPADDING;
			_rightOffset1 += _actionIcon.getWidth() + HPADDING;
		}		
	
		_labelField.layout(width - _leftOffset - _rightOffset, height);
		_labelHeightTexto1 = _labelField.getHeight();
		_labelHeightTexto2 = _labelField2.getHeight();
		int labelWidth = _labelField.getWidth();

		if (_labelField.isStyle(DrawStyle.HCENTER)) {
			_leftOffset = (width - labelWidth) / 2;
		} else if (_labelField.isStyle(DrawStyle.RIGHT)) {
			_leftOffset = width - labelWidth - HPADDING - _rightOffset;
		}

		_labelField2.layout(width - _leftOffset1 - _rightOffset1, height-5);
		_labelHeightTexto2 = _labelField2.getHeight();
		int labelWidth1 = _labelField2.getWidth();

		if (_labelField2.isStyle(DrawStyle.HCENTER)) {
			_rightOffset1 = (width - labelWidth1);
		} else if (_labelField2.isStyle(DrawStyle.RIGHT)) {
			_rightOffset1 = (width - labelWidth1);
		}

		int extraVPaddingNeeded;

		if(!_esdobleFila)
		{
			setExtent(width, _bitmapHeight + 5);

		}
		else
		{
			extraVPaddingNeeded = 8;//(_labelHeightTexto2 + _labelHeightTexto1);
			setExtent(width, _labelHeightTexto1 + _labelHeightTexto2+ extraVPaddingNeeded);
		}
	}

	public void setText(String text) {
		_labelField.setText(text);
		updateLayout();
	}


	protected void paint(Graphics g) {
		// Left Bitmap
		if (_leftIcon != null) {
			g.setGlobalAlpha(255); // Sin Transparencia la imagen de la izquierda
			g.drawBitmap(HPADDING, VPADDING, _leftIcon.getWidth(),_leftIcon.getHeight(), _leftIcon, 0, 0);
		}

		// Text
		try {
			// Seteo el color de letrea y la opacidad
			g.setColor(0x1D4175); //Azul Banesco
			g.setGlobalAlpha(255); // Sin transparencia las letras

			if(!_esdobleFila)
			{
				try {
					int displayWidth = Display.getWidth();

					if(displayWidth < 480)
						_tamanofuente = 17;
					else
						_tamanofuente = 26;

					int style2 = 0;

					style2 = Font.STYLE_PLAIN;

					FontFamily fontFamily1;
					fontFamily1 = FontFamily.forName(fontFamily);
					g.setFont(fontFamily1.getFont(style2,_tamanofuente));

				}
				catch (ClassNotFoundException e) {
					System.out.println("Error Label" + e.getMessage());
					e.printStackTrace();
				}

				g.pushRegion(_leftOffset, (getHeight() - _labelHeightTexto1) / 2,
						getWidth() - _leftOffset - _rightOffset, _labelHeightTexto1, 0, 0);

				_labelField.paint(g);
			}
			else
			{
				try {
					int displayWidth = Display.getWidth();

					if(displayWidth < 480)
						_tamanofuente = 17;
					else
						_tamanofuente = 26;

					int style1 = 0;
					int style2 = 0;

					style1 = Font.STYLE_BOLD;
					style2 = Font.STYLE_PLAIN;
					
					FontFamily fontFamily1;
					fontFamily1 = FontFamily.forName(fontFamily);
					g.setFont(fontFamily1.getFont(style2,_tamanofuente));
										
					g.pushRegion(_leftOffset, _paddingEntreTexto,
							getWidth() - _leftOffset - _rightOffset, _labelHeightTexto1 + _labelHeightTexto2 +_paddingEntreTexto, 0, 0);

					g.setFont(fontFamily1.getFont(style1,_tamanofuente));
					g.drawText(_labelField.getText(),0, 0);
					
					g.setFont(fontFamily1.getFont(style2,_tamanofuente));
					g.drawText(_labelField2.getText(),0, _labelHeightTexto1);
				}
				catch (ClassNotFoundException e) {
					System.out.println("Error Label" + e.getMessage());
					e.printStackTrace();
				}
			}
		} finally {
			g.popContext();
		}

		// Right (Action) Bitmap
		if (_actionIcon != null) {
			g.drawBitmap(getWidth() - HPADDING - _actionIcon.getWidth(),
					(getHeight() - _actionIcon.getHeight()) / 2,
					_actionIcon.getWidth(), _actionIcon.getHeight(), _actionIcon, 0, 0);
		}
	}


	protected void paintBackground(Graphics g) {

		int background;
		int alpha;
//		if(_esPar)
//		{
			background = g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS) ? _fondoSelected: Color.WHITE;
			alpha = g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS) ? 158: 0;
//		}
//		else
//		{
//			background = g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS) ? _fondoSelected: _fondo;
//			alpha = g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS) ? 158: 184;
//		}

		g.setGlobalAlpha(alpha);

		try {
			if (_drawPosition == 0) {
				// Top
				g.setColor(background);
				g.fillRoundRect(0, 0, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS);
			} else if (_drawPosition == 1) {
				// Bottom
				g.setColor(background);
				g.fillRoundRect(0, -CORNER_RADIUS, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS);
			} else if (_drawPosition == 2) {
				// Middle
				g.setColor(background);
				g.fillRoundRect(0, -CORNER_RADIUS, getWidth(), getHeight() + 2 * CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS);
			} else {
				// Single
				g.setColor(background);
				g.fillRoundRect(0, 0, getWidth(), getHeight(), CORNER_RADIUS, CORNER_RADIUS);
			}
		} finally {
			g.setColor(_fondo);
		}
	}


	protected void drawFocus( Graphics g, boolean on )
	{

		if( _drawPosition < 0 ) {
			//super.drawFocus( g, on ); //Si se pone el selected es el azul por defecto y no el color que quiere banesco
			if( on ) {
				g.setDrawingStyle( Graphics.DRAWSTYLE_FOCUS, true );
			}
			paintBackground( g );
			paint( g );
		} else {
			boolean oldDrawStyleFocus = g.isDrawingStyleSet( Graphics.DRAWSTYLE_FOCUS );
			try {
				if( on ) {
					g.setDrawingStyle( Graphics.DRAWSTYLE_FOCUS, true );
				}
				paintBackground( g );
				paint( g );
			} finally {
				g.setDrawingStyle( Graphics.DRAWSTYLE_FOCUS, oldDrawStyleFocus );
			}
		}
	}


	protected boolean keyChar(char character, int status, int time) {
		if (character == Characters.ENTER) {
			clickButton();
			return true;
		}
		return super.keyChar(character, status, time);
	}

	protected boolean navigationClick(int status, int time) {
		clickButton();
		return true;
	}

	protected boolean trackwheelClick(int status, int time) {
		clickButton();
		return true;
	}

	// #ifndef VER_4.1.0 | 4.0.0
	protected boolean invokeAction(int action) {
		switch (action) {
		case ACTION_INVOKE: {
			clickButton();
			return true;
		}
		}
		return super.invokeAction(action);
	}

	// #endif

	/**
	 * A public way to click this button
	 */
	public void clickButton() {
		fieldChangeNotify(0);
	}

	// #ifndef VER_4.6.1 | VER_4.6.0 | VER_4.5.0 | VER_4.2.1 | VER_4.2.0
	protected boolean touchEvent(TouchEvent message) {
		int x = message.getX(1);
		int y = message.getY(1);
		if (x < 0 || y < 0 || x > getExtent().width || y > getExtent().height) {
			// Outside the field
			return false;
		}
		switch (message.getEvent()) {

		case TouchEvent.UNCLICK:
			clickButton();
			return true;
		}
		return super.touchEvent(message);
	}

	// #endif

	public void setDirty(boolean dirty) {
	}

	public void setMuddy(boolean muddy) {
	}

	private static class MyLabelField extends LabelField {

		private int _tamanofuente;
		private String fontFamily = "BBAlpha Sans";

		public MyLabelField(String text, long style, boolean textoResaltado) {
			super(text, style);

			//			if(textoResaltado)
			//			{
			//				setFont(getFont().derive(Font.STYLE_BOLD,24));
			//			}
			try 
			{
				int displayWidth = Display.getWidth();

				if(displayWidth < 480)
					_tamanofuente = 17;
				else
					_tamanofuente = 26;

				int style2 = 0;
				
				if(textoResaltado)
					style2 = Font.STYLE_BOLD;
				else
					style2 = Font.STYLE_PLAIN;

				FontFamily fontFamily1;
				fontFamily1 = FontFamily.forName(fontFamily);
				this.setFont(fontFamily1.getFont(style2,_tamanofuente));
			}
			catch (ClassNotFoundException e) {
				System.out.println("Error Label" + e.getMessage());
				e.printStackTrace();
			}
		}

		public void layout(int width, int height) {
			super.layout(width, height);
		}

		public void paint(Graphics g) {
			super.paint(g);
		}
	}

}
